home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <fcntl.h>
- /*
- Build a Sphinx Color-map
-
- Ce programme construit un fichier color-map au format de sphinx a partir des
- donnees lues sur stdin. Le nom du fichier color-map a construire doit etre
- passe en parametre. Le format des donnees sur stdin est :
-
- b i red green blue
-
- b : nom du banc (r : red, g : green, b : blue)
- i : numero d'entree (de 0 a 255)
- red : niveau de rouge (de 0 a 255)
- green : niveau de vert (de 0 a 255)
- blue : niveau de bleu (de 0 a 255)
-
- Les niveaux non definis sont mis a zero.
-
- */
-
- struct color_map
- {
- short red [256];
- short green [256];
- short blue [256];
- };
-
- typedef struct color_map color_map;
-
- main(argc,argv)
- int argc;
- char * argv [];
- {
- int fd_out;
- color_map red_map, green_map, blue_map;
-
- /* Le nom du fichier color-map doit etre passe en argument */
- if (argc != 2)
- {
- fprintf(stderr,"Usage is %s color-map-file\n",argv[0]);
- exit(1);
- }
-
- /* Ouverture du fichier color-map */
- fd_out = open(argv[1],O_WRONLY | O_CREAT);
- if (fd_out < 0)
- {
- fprintf(stderr,"Cannot write %s\n",argv[1]);
- exit(1);
- }
-
- /* Creation de la color map a partir de stdin */
- create_map(&red_map,&green_map,&blue_map);
-
- /* Ecriture du fichier color-map */
- write_map(fd_out,&blue_map);
- write_map(fd_out,&green_map);
- write_map(fd_out,&red_map);
-
- exit(0);
- }
-
- /* Creation de la color map a partir de stdin */
-
- create_map(red_map,green_map,blue_map)
- color_map * red_map, * green_map, * blue_map;
- {
- int i, n, red, green, blue;
- int line_number;
- char bank;
- color_map * map;
- char buffer [128];
-
- clear_map(red_map);
- clear_map(green_map);
- clear_map(blue_map);
-
- line_number = 0;
- while (gets(buffer) != NULL)
- {
- n = sscanf(buffer,"%c %d %d %d %d",
- &bank,&i,&red,&green,&blue);
- if ((n != 5) || out_of_range(i) ||
- out_of_range(red) || out_of_range(green) || out_of_range(blue))
- error(line_number);
- line_number++;
- switch(bank)
- {
- case 'r' : map = red_map; break;
- case 'g' : map = green_map; break;
- case 'b' : map = blue_map; break;
- default : error(line_number);
- }
- map->red [i] = red;
- map->green [i] = green;
- map->blue [i] = blue;
- }
- }
-
- out_of_range(val)
- int val;
- {
- return((val < 0) || (val > 255));
- }
-
- error(line_number)
- int line_number;
- {
- fprintf(stderr,"Bad input format at line number %d\n",line_number);
- exit(1);
- }
-
- clear_map(map)
- color_map * map;
- {
- int i;
- for (i = 0; i < 256; i++)
- map->red [i] =
- map->green [i] =
- map->blue [i] = 0;
- }
-
- write_map(fd_out,map,name)
- int fd_out;
- color_map * map;
- char * name;
- {
- int n;
- n = write(fd_out,map->red,sizeof(map->red));
- if (n != (sizeof(map->red))) return(-1);
- n = write(fd_out,map->green,sizeof(map->green));
- if (n != (sizeof(map->green))) return(-1);
- n = write(fd_out,map->blue,sizeof(map->blue));
- if (n != (sizeof(map->blue))) return(-1);
- return(0);
- }
-
-